
// 耗时操作

/**
 * gen 和 async 比较
 */
const timeConsume = function () {
  return new Promise(function (resolve, reject) {
        setTimeout(() => {
            resolve(5);
        }, 2000)
  });
};
const gen = function* () {
    const f1 = yield timeConsume();
    f1.value.then((res, err) => {
        console.log(res)
    });
};
  
let it = gen();
let res = it.next();
it.next(res);

const asyncReadFile = async function () {
    const f2 = await timeConsume();
    console.log(f2.toString());
};

asyncReadFile();

/**
 * async 特性
 */
var a1 = async function () {
    return 1;
};
console.log(a1());

var a1 = async function () {
    var f2 = await timeConsume();
    return f2;
};
console.log(a1());

var a2 = async function () {
    throw new Error(123);
    await timeConsume();
};
console.log(a2());


var a2 = async function () {
    console.log(await Promise.reject(123));
};
a2();

async function async1(){
    console.log('async1 start')
    await async2()
    console.log('async1 end')
}

async function async2(){
    console.log('async2')
}

console.log('script start')

setTimeout(function(){
    console.log('setTimeout') 
},0)  

async1();

new Promise(function(resolve){
    console.log('promise1')
    resolve();
}).then(function(){
    console.log('promise2')
})

console.log('script end')


let p = Promise.resolve();
// console.log(Promise.resolve().then(()=>{}))
// VM100:1 Promise {<pending>}

  // 使用then方法，添加所有动画
for(let anim of [1,2,3,4,5]) {
    console.log(anim)
    p = p.then(function(val) {
        console.log(val, anim);
        return anim;
    });
}